package com.hunttown.mes.impl;

import com.hunttown.mes.convert.AnalysisManageRightRoleMenuRelConverts;
import com.hunttown.mes.rpc.api.AnalysisManageRightRoleMenuRelRpcService;
import com.hunttown.mes.rpc.domain.AnalysisManageRightRoleMenuRelDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisManageRightRoleMenuRelDTOQuery;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.service.AnalysisManageRightRoleMenuRelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisManageRightRoleMenuRelConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:06:03
 */
@Service
public class AnalysisManageRightRoleMenuRelRpcServiceImpl implements AnalysisManageRightRoleMenuRelRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisManageRightRoleMenuRelRpcServiceImpl.class);

    private final AnalysisManageRightRoleMenuRelService analysisManageRightRoleMenuRelService;

    @Autowired
    public AnalysisManageRightRoleMenuRelRpcServiceImpl(AnalysisManageRightRoleMenuRelService analysisManageRightRoleMenuRelService) {
        this.analysisManageRightRoleMenuRelService = analysisManageRightRoleMenuRelService;
    }

    @Override
    public AnalysisManageRightRoleMenuRelDTO insert(AnalysisManageRightRoleMenuRelDTO analysisManageRightRoleMenuRelDTO) {
        try {
            return convert(analysisManageRightRoleMenuRelService.insert(AnalysisManageRightRoleMenuRelConverts.convert(analysisManageRightRoleMenuRelDTO)));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleMenuRel create failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleMenuRel create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisManageRightRoleMenuRelDTO analysisManageRightRoleMenuRelDTO) {
        try {
            return analysisManageRightRoleMenuRelService.updateInfoById(AnalysisManageRightRoleMenuRelConverts.convert(analysisManageRightRoleMenuRelDTO));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleMenuRel update by id failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleMenuRel update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisManageRightRoleMenuRelDTOQuery analysisManageRightRoleMenuRelDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageRightRoleMenuRelDTOQuery.getW_id() != null && analysisManageRightRoleMenuRelDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisManageRightRoleMenuRelDTOQuery.getW_id());
            }

            if (analysisManageRightRoleMenuRelDTOQuery.getIds() != null && !analysisManageRightRoleMenuRelDTOQuery.getIds().equals("")) {
                query.put("ids", analysisManageRightRoleMenuRelDTOQuery.getIds());
            }

            if (analysisManageRightRoleMenuRelDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisManageRightRoleMenuRelDTOQuery.getDeleteFlag());
            }

            if (analysisManageRightRoleMenuRelDTOQuery.getRoleIds() != null && !analysisManageRightRoleMenuRelDTOQuery.getRoleIds().equals("")) {
                query.put("roleIds", analysisManageRightRoleMenuRelDTOQuery.getRoleIds());
            }

            return analysisManageRightRoleMenuRelService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleMenuRel update by query failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleMenuRel update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageRightRoleMenuRelDTO selectById(Integer id) {
        try {
            return convert(analysisManageRightRoleMenuRelService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleMenuRel select by id failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleMenuRel select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageRightRoleMenuRelDTO selectObjByQuery(AnalysisManageRightRoleMenuRelDTOQuery analysisManageRightRoleMenuRelDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(analysisManageRightRoleMenuRelService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleMenuRel select obj by query failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleMenuRel select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisManageRightRoleMenuRelDTO> getListForPage(AnalysisManageRightRoleMenuRelDTOQuery analysisManageRightRoleMenuRelDTOQuery) {
        Query query = Query.page(analysisManageRightRoleMenuRelDTOQuery.getPn(), analysisManageRightRoleMenuRelDTOQuery.getPageSize());

        if (analysisManageRightRoleMenuRelDTOQuery.getIds() != null && !analysisManageRightRoleMenuRelDTOQuery.getIds().equals("")) {
            query.put("ids", analysisManageRightRoleMenuRelDTOQuery.getIds());
        }

        if (analysisManageRightRoleMenuRelDTOQuery.getLikeName() != null && !analysisManageRightRoleMenuRelDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisManageRightRoleMenuRelDTOQuery.getLikeName());
        }

        if (analysisManageRightRoleMenuRelDTOQuery.getRoleId() != null && analysisManageRightRoleMenuRelDTOQuery.getRoleId() > 0) {
            query.put("roleId", analysisManageRightRoleMenuRelDTOQuery.getRoleId());
        }

        if (analysisManageRightRoleMenuRelDTOQuery.getOrderBy() != null && analysisManageRightRoleMenuRelDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisManageRightRoleMenuRelDTOQuery.getOrderBy());
        }

        List<AnalysisManageRightRoleMenuRelDTO> list = convert(analysisManageRightRoleMenuRelService.selectByQuery(query));
        Page<AnalysisManageRightRoleMenuRelDTO> page = Page.from(analysisManageRightRoleMenuRelDTOQuery.getPn(), analysisManageRightRoleMenuRelDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisManageRightRoleMenuRelService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisManageRightRoleMenuRelService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleMenuRel delete failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleMenuRel delete failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean deletePhysicalByRoleId(Integer roleId) {
        try {
            return analysisManageRightRoleMenuRelService.deletePhysicalByRoleId(roleId);
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleMenuRel delete by roleid failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleMenuRel delete by roleid failed, Please order your code.", e);
        }
    }

    @Override
    public void insertForBatch(List<AnalysisManageRightRoleMenuRelDTO> list) {
        try {
            analysisManageRightRoleMenuRelService.insertForBatch(AnalysisManageRightRoleMenuRelConverts.converto(list));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleMenuRel insert for batch failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleMenuRel insert for batch failed, Please order your code.", e);
        }
    }
}